package com.gcc.web;

import com.gcc.pojo.Cart;
import com.gcc.pojo.Order;
import com.gcc.pojo.OrderItem;
import com.gcc.pojo.User;
import com.gcc.service.OrderService;
import com.gcc.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BaseServlet {
    /**
     * 生成订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private OrderService orderService=new OrderServiceImpl();


        //查看我的订单
    protected void showMyOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user=(User) req.getSession().getAttribute("user");
        if(user.getUsername()!="admin")
        {
            List<Order> Myorders=orderService.showMyOrders(user.getId());
            req.setAttribute("Myorders",Myorders);
            req.getRequestDispatcher("/pages/order/order.jsp").forward(req,resp);

        }

    }
    //签收订单
    protected void receiveOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId=req.getParameter("orderId");
        orderService.receiveOrder(orderId);
        resp.sendRedirect(req.getContextPath()+"/orderServlet?action=showMyOrder");
    }
        //查看订单详情
    protected void showOrderDetails(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user= (User) req.getSession().getAttribute("user");
        if("admin".equals(user.getUsername()))
        {

            String orderId= (String) req.getParameter("orderId");
            List<OrderItem> detais=orderService.showOrderDetais(orderId);
            req.setAttribute("detais",detais);
            req.getRequestDispatcher("/pages/order/orderItem_manager.jsp").forward(req,resp);
        }
        else
        {
            String orderId= (String) req.getParameter("orderId");
            List<OrderItem> myDetais=orderService.showOrderDetais(orderId);
            req.setAttribute("myDetais",myDetais);
            req.getRequestDispatcher("/pages/order/orderItem_client.jsp").forward(req,resp);
        }




    }
        //发货
    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String orderId=req.getParameter("orderId");
        orderService.sendOrder(orderId);
        resp.sendRedirect(req.getContextPath()+"/orderServlet?action=showAllOrders");
    }
        //查看所有订单
    protected void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        User user= (User) req.getSession().getAttribute("user");
        if("admin".equals(user.getUsername()))
        {//是管理员，可以获取所有订单
            List<Order> orders=orderService.showAllOrders();
            req.setAttribute("orders",orders);
            req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req,resp);
        }


    }
        protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取Cart购物车对象和UserId
        Cart cart= (Cart) req.getSession().getAttribute("cart");
        User loginUser = (User) req.getSession().getAttribute("user");
        if(loginUser==null)
        {
            req.getRequestDispatcher("pages/user/login.jsp").forward(req,resp);
            return;
        }
        Integer userId=loginUser.getId();
        //需要调用orderService.createOrder(Cart,UserId);

               String orderId = orderService.createOrder(cart,userId);


            //req.setAttribute("orderId",orderId);
        req.getSession().setAttribute("orderId",orderId);
        //请求转发到/pages/cart/checkout.jsp
        resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");
    }
}
